var TcRevisit = {
    id: "TcRevisitTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.revisitStatus == 2) {
        return {css: {"background-color": "#fff1d5", "overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

TcRevisit.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {
            title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='color: red;display: flex;align-items: center;' onclick='TcRevisit.consultantToDetail(" + '"' + row.customerId + '"' + ")'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '联系电话', field: 'customerPhone', align: 'center', valign: 'middle', width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '创建人', field: 'createName', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访人员', field: 'revisit', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访内容', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 200,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访结果', field: 'revisitContent', align: 'center', valign: 'middle', width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '计划回访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '完成时间', field: 'updateTime', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访状态', field: 'revisitStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value === 1 ? '待计划' : value === 2 ? '回访完成' : value === 3 ? '回访未完成' : value === 4 ? '回访取消' : '' ;
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '操作', field: 'id', align: 'center', valign: 'middle', width: 60,
            formatter: function (value, row, index) {
                var ids = row.id + "," + row.reservationsId + "," + row.customerId + "," + row.revisitTime;
                if (row.revisitStatus == 1) {
                    return '<button title="编辑回访" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="TcRevisit.followRevision(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-edit"></span></button>' +
                        '<button title="删除回访" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="TcRevisit.deletefollowRevision(' + "'" + row.id + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-trash"></span></button> '
                } else {
                    return '<button title="查看回访" style="background: #ffffff; border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="TcRevisit.lookfollowRevision(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-eye-open"></span></button> '
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        }
    ];
    return columns;
};

TcRevisit.today = function () { // 点击上面状态筛选表格
    $("#startRevisitTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    TcRevisit.search();
};

TcRevisit.tomorrow = function () { // 明天
    $("#startRevisitTime").val(setDateTime(new Date(), -1) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(), -1) + " " + "23:59:59");
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    TcRevisit.search();
};

TcRevisit.thisWeek = function () { // 本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startRevisitTime").val(setDateTime(monday, 0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(sunday, 0) + " " + "23:59:59");
    $(".timeBox > div button").eq(2).addClass('actionClick').siblings().removeClass('actionClick');
    TcRevisit.search();
};

TcRevisit.consultantToDetail = function (id) {
    var index = layer.open({
        type: 2,
        title: '&nbsp',
        area: ['100%', '100%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/10/0'
    });
    this.layerIndex = index;
};

TcRevisit.deletefollowRevision = function (ids) {//删除回访
    if (confirm("确认删除本条回访？")) {
        $.ajax({
            url: Feng.ctxPath + "/tcRevisit/delete_tcFollow",
            type: "post",
            data: {
                ids: ids
            },
            success: function (data) {
                Feng.success('操作成功!');
                window.TcRevisit.search();
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
};

TcRevisit.followRevision = function (ids) {//编辑回访
    var index = layer.open({
        type: 2,
        title: '回访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/tcRevisit/tc_follow/' + ids
    });
    this.layerIndex = index;
};

TcRevisit.lookfollowRevision = function (ids) {//查看回访
    var index = layer.open({
        type: 2,
        title: '回访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/tcRevisit/tc_lookfollow/' + ids
    });
    this.layerIndex = index;
};

TcRevisit.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TcRevisit.seItem = selected[0];
        return true;
    }
};

TcRevisit.search = function () {//查询预约客户列表
    TcRevisit.table.refresh({query: TcRevisit.formParams()});
    TcRevisit.total();
};

TcRevisit.total = function () {//人数统计
    var reach = $("#doctorTcInputPublic", parent.document).val();
    var startRevisitTime = $("#startRevisitTime").val();  //开始回访时间
    var endRevisitTime = $("#endRevisitTime").val();  //结束回访时间
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var startUpdateTime = $("#startUpdateTime").val();
    var endUpdateTime = $("#endUpdateTime").val();
    var followStatus = $("#followStatus").val();
    var consumptionStatus = $("#consumptionStatus").val();
    var revisit = $("#revisit").val();  //回访人员
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/tcRevisit/total",
        data: {
            reach: reach,
            startRevisitTime: startRevisitTime,
            endRevisitTime: endRevisitTime,
            startTime: startTime,
            endTime: endTime,
            startUpdateTime: startUpdateTime,
            endUpdateTime: endUpdateTime,
            followStatus: followStatus,
            consumptionStatus: consumptionStatus,
            revisit: revisit,
        },
        dataType: "json",
        success: function (data) {
            $("#totalNum").html(data[0].countNum);//总人数
            $("#planned").html(data[1].countNum);//待计划
            $("#finished").html(data[2].countNum);//已完成
            $("#unfinished").html(data[3].countNum);//未完成
            $("#tocancel").html(data[4].countNum);//取消
        }
    });
};

TcRevisit.inquire = function () {//人数统计 + 表格刷新
    TcRevisit.search();
};

TcRevisit.formParams = function () {//查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['reach'] = $("#doctorTcInputPublic", parent.document).val().trim();
    queryData['revisitStatus'] = $("#revisitStatus").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startUpdateTime'] = $("#startUpdateTime").val();
    queryData['endUpdateTime'] = $("#endUpdateTime").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    queryData['revisit'] = $("#revisit").val();
    queryData['createName'] = $("#createName").val();
    queryData['outpatientName'] = $("#outpatientName",parent.document).val();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    return queryData;
};

TcRevisit.totalAllNumber = function (obj) {//根据点击上面状态刷新表格
    $("#revisitStatus").val(obj);
    TcRevisit.inquire();
};

TcRevisit.resetSearch = function () {//重置
    $("#doctorTcInputPublic", parent.document).val("");
    $("#startRevisitTime").val("");
    $("#endRevisitTime").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#startUpdateTime").val("");
    $("#endUpdateTime").val("");
    $("#consumptionStatus").val("");
    $("#followStatus").val("");
    $("#revisit").val("");
    $("#createName").val("");
    TcRevisit.inquire();
};

TcRevisit.time = function () {//初始化赋值时间 + 请求数据
    $("#revisitTimeTwo").val(setDateTime(new Date(), 0));
    var seletetcName = $('#loginName').val();
    if (seletetcName != "") {
        $("#revisit option[value='" + seletetcName + "']").attr("selected", "selected");
    }
    TcRevisit.search();
};

$("#doctorTcInputPublic", parent.document).click(function () {//主页搜索 tc
    TcRevisit.inquire();
});

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#TcRevisitTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = TcRevisit.initColumn();
    var table = new BSTable(TcRevisit.id, "/tcRevisit/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(TcRevisit.formParams());
    TcRevisit.table = table.init();
    checkDate = function () {TcRevisit.inquire();};
    TcRevisit.today();
    TcRevisit.time();
    TcRevisit.totalAllNumber();
    $("#totalAllNumber").addClass('actionClick').siblings().removeClass('actionClick');
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {TcRevisit.inquire();});
    phonePrivileges();
});